<html>
<head>
    <script src="../OLLoader.js"></script>
    <script type="text/javascript">

    function test_initialize(t) {
        t.plan(4);

        var options = {
            url: "http://localhost/wmts",
            layers: ["foo"],
            formatOptions: {
                foo: "bar"
            }
        };
        var control = new OpenLayers.Control.WMTSGetFeatureInfo(options);
        t.ok(control instanceof OpenLayers.Control.WMTSGetFeatureInfo,
             "new OpenLayers.Control.WMTSGetFeatureInfo returns an instance");
        t.eq(control.layers, ["foo"],
             "constructor layers"
            );
        t.ok(control.format instanceof OpenLayers.Format.WMSGetFeatureInfo, "format created");
        t.eq(control.format.foo, "bar", "format options used")
    }

    function test_clickCallBack_option(t) {
        t.plan(9);

        var control;

        control = new OpenLayers.Control.WMTSGetFeatureInfo({
            hover: true
        });
        t.ok(control.handler instanceof OpenLayers.Handler.Hover,
             'constructor creates hover handler');
        t.ok(control.handler.callbacks["move"] === control.cancelHover,
             'constructor registers proper "move" callback in handler');
        t.ok(control.handler.callbacks["pause"] === control.getInfoForHover,
             'constructor registers proper "pause" callback in handler');

        control = new OpenLayers.Control.WMTSGetFeatureInfo();
        t.ok(control.handler instanceof OpenLayers.Handler.Click,
             'constructor creates click handler');
        t.ok(control.handler.callbacks["click"] === control.getInfoForClick,
             'constructor registers proper "click" callback in handler');

        control = new OpenLayers.Control.WMTSGetFeatureInfo({
            clickCallback: "rightclick"
        });
        t.ok(control.handler.callbacks["rightclick"] === control.getInfoForClick,
             'constructor registers proper "rightclick" callback in handler');

        control = new OpenLayers.Control.WMTSGetFeatureInfo({
            clickCallback: "dblclick",
            handlerOptions: {
                click: {
                    "single": false,
                    "double": true
                }
            }
        });
        t.ok(control.handler.callbacks["dblclick"] === control.getInfoForClick,
             'constructor registers proper "dblclick" callback in handler');
        t.eq(control.handler["single"], false,
             'constructor sets "single" to false in handler');
        t.eq(control.handler["double"], true,
             'constructor sets "double" to true in handler');
     }

    function test_destroy(t) {
        t.plan(2);
        var map = new OpenLayers.Map("map");
        var click = new OpenLayers.Control.WMTSGetFeatureInfo({
            url: 'http://localhost/wms',
            layers: ["foo"]
        });

        var hover = new OpenLayers.Control.WMTSGetFeatureInfo({
            url: 'http://localhost/wms',
            layers: ["foo"],
            hover: true
        });

        click.handler.deactivate = function() {
            t.ok(true,
                 "control.deactivate calls deactivate on click handler");
        };
        hover.handler.deactivate = function() {
            t.ok(true,
                 "control.deactivate calls deactivate on hover handler");
        };
        click.destroy();
        hover.destroy();
    }

    function test_click(t) {
        t.plan(4);
        var map = new OpenLayers.Map('map');

        // mock up active control
        var control = new OpenLayers.Control.WMTSGetFeatureInfo();
        map.addControl(control);
        control.activate();

        control.request = function(position) {
            t.eq(position.x, 200,
                 "x position is as expected");
            t.eq(position.y, 125,
                 "y position is as expected");
        };

        control.getInfoForClick({xy: {x: 200, y: 125}});
        control.getInfoForHover({xy: {x: 200, y: 125}});
    }

    function test_beforegetfeatureinfo_event(t) {
        t.plan(2);
        var map = new OpenLayers.Map({
            div: "map",
            allOverlays: true,
            layers: [
                new OpenLayers.Layer.WMTS({
                    name: "Test WMTS 1", 
                    url: "/testwmts/",
                    layer: "test1",
                    style: "",
                    matrixSet: "set-id",
                    isBaseLayer: false
                }),
                new OpenLayers.Layer.WMTS({
                    name: "Test WMTS 2", 
                    url: "/testwmts/",
                    layer: "test2",
                    style: "",
                    matrixSet: "set-id",
                    isBaseLayer: false
                })
            ],
            center: new OpenLayers.LonLat(0, 0),
            zoom: 0
        });

        var log = [];

        // test click
        var click = new OpenLayers.Control.WMTSGetFeatureInfo({
            drillDown: true,
            eventListeners: {
                beforegetfeatureinfo: function(evt) {
                    log.push({xy: evt.xy});
                }
            }
        });
        map.addControl(click);
        click.activate();
        click.getInfoForClick({xy: {x: 200, y: 125}});
        t.eq(log.length, 2, "click: beforegetfeatureinfo triggered twice");
        log = [];
        click.deactivate();

        // test hover
        var hover = new OpenLayers.Control.WMTSGetFeatureInfo({
            hover: true,
            eventListeners: {
                beforegetfeatureinfo: function(evt) {
                    log.push({xy: evt.xy});
                }
            }
        });
        map.addControl(hover);
        hover.activate();
        xy = {x: 70, y: 70};
        hover.getInfoForHover({xy: {x: 70, y: 70}});
        t.eq(log.length, 1, "hover: beforegetfeatureinfo triggered once");
        log = [];
        hover.deactivate();
        
        map.destroy();
    }

    function test_activate(t) {
        t.plan(4);
        var map = new OpenLayers.Map("map");
        var click = new OpenLayers.Control.WMTSGetFeatureInfo({
            url: 'http://localhost/wms',
            layers: ['ns:type']
        });
        var hover = new OpenLayers.Control.WMTSGetFeatureInfo({
            url: 'http://localhost/wms',
            featureType: 'type',
            featureNS: 'http://localhost/ns',
            layers: 'ns:type',
            hover: true
        });
        map.addControl(click);
        map.addControl(hover);
        t.ok(!click.handler.active,
             "click handler is not active prior to activating control");
        t.ok(!hover.handler.active,
             "hover handler is not active prior to activating control");
        click.activate();
        hover.activate();
        t.ok(click.handler.active,
             "click handler is active after activating control");
        t.ok(hover.handler.active,
             "hover handler is active after activating control");
    }

    function test_deactivate(t) {
        t.plan(2);
        var map = new OpenLayers.Map("map");
        var click = new OpenLayers.Control.WMTSGetFeatureInfo({
            url: 'http://localhost/wms',
            featureType: 'type',
            featureNS: 'http://localhost/ns',
            layers: 'ns:type'
        });
        var hover = new OpenLayers.Control.WMTSGetFeatureInfo({
            url: 'http://localhost/wms',
            featureType: 'type',
            featureNS: 'http://localhost/ns',
            layers: 'ns:type'
        });
        map.addControl(click);
        map.addControl(hover);
        click.activate();
        hover.activate();

        click.handler.deactivate = function() {
            t.ok(true,
                 "control.deactivate calls deactivate on click handler");
            OpenLayers.Handler.Click.prototype.deactivate.apply(this, arguments);
        };
        hover.handler.deactivate = function() {
            t.ok(true,
                 "control.deactivate calls deactivate on hover handler");
            OpenLayers.Handler.Hover.prototype.deactivate.apply(this, arguments);
        };
        click.deactivate();
        hover.deactivate();
    }

    function test_getInfoForClick(t) {

        t.plan(13);
        var map = new OpenLayers.Map({
            div: "map",
            getExtent: function() {
                return new OpenLayers.Bounds(-180,-90,180,90);
            }
        });

        var a = new OpenLayers.Layer.WMTS({
            url: "http://a.example.com/wmts",
            layer: "a",
            matrixSet: "bar",
            style: "default"
        });

        var b = new OpenLayers.Layer.WMTS({
            url: "http://b.example.com/wmts",
            layer: "b",
            matrixSet: "bar",
            style: "default",
            isBaseLayer: false
        });

        var c = new OpenLayers.Layer.WMTS({
            url: ["http://c1.example.com/wmts", "http://c2.example.com"],
            layer: "c",
            matrixSet: "bar",
            style: "default",
            isBaseLayer: false
        });
        map.addLayers([a, b, c]);
        map.zoomToMaxExtent();

        var control = new OpenLayers.Control.WMTSGetFeatureInfo({
            layers: [a, b, c]
        });
        map.addControl(control);
        control.activate();

        // log calls to GET
        var log;
        var _request = OpenLayers.Request.GET;
        OpenLayers.Request.GET = function(options) {
            log.push(options);
        };

        // query first layer (drillDown false)
        log = [];
        control.drillDown = false;
        control.queryVisible = false;
        control.getInfoForClick({xy: {x: 200, y: 125}});
        t.eq(log.length, 1, "one requests issued");
        t.eq(log[0].url, "http://c1.example.com/wmts", "{drillDown: false} correct url");
        t.eq(log[0].params["LAYER"], "c", "{drillDown: false} correct layer parameter");

        // query all layers
        log = [];
        control.drillDown = true;
        control.queryVisible = false;
        control.getInfoForClick({xy: {x: 200, y: 125}});
        t.eq(log.length, 3, "three requests issued");
        t.eq(log[0].url, "http://c1.example.com/wmts", "[c] correct url");
        t.eq(log[0].params["LAYER"], "c", "[c] correct layer parameter");
        t.eq(log[1].url, "http://b.example.com/wmts", "[b] correct url");
        t.eq(log[1].params["LAYER"], "b", "[b] correct layer parameter");
        t.eq(log[2].url, "http://a.example.com/wmts", "[a] correct url");
        t.eq(log[2].params["LAYER"], "a", "[a] correct layer parameter");

        // query only visible layers
        log = [];
        control.drillDown = true;
        control.queryVisible = true;
        b.setVisibility(false);
        control.getInfoForClick({xy: {x: 200, y: 125}});
        t.eq(log.length, 2, "two requests issued");
        t.eq(log[0].url, "http://c1.example.com/wmts", "correct url for second visible layer");
        t.eq(log[1].url, "http://a.example.com/wmts", "correct url for first visible layer");

        // clean up
        OpenLayers.Request.GET = _request;
        map.destroy();

    }

    </script>
</head>
<body>
    <div id="map" style="width: 400px; height: 250px;"/>
</body>
</html>
